<template>
  <div>
    <ZgPage
      ref="page"
      title="发票管理"
      addBtnName="开具发票"
      :pageOpt="pageOpt"
      :scrollX="2500"
      :addOpt="{title:'开具发票'}"
      :extraBtns="[{key:'redInvoice',name:'红冲',isShow:obj=>{return obj.billingType == 0 &&obj.invoiceStatus == 10 }}]"
      @extraBtn="onExtraBtnClick"
      @search="onSearch"
      @add="onAdd"
      @resetCondition="onResetCondition"
      @searchWithCondition="onSearchWithCondition"
    >
      <template slot="columns">
        <a-table-column :width="150" dataIndex="invoiceType" title="发票类型">
          <template slot-scope="text,record">
            <ZgDicHolder :code="record.invoiceType" parentCode="invoiceType"/>
          </template>
        </a-table-column>
        <a-table-column :width="150" dataIndex="invoiceStatus" title="发票状态">
          <template slot-scope="text,record">
            <ZgDicHolder :code="record.invoiceStatus" parentCode="invoiceStatus"/>
          </template>
        </a-table-column>
        <a-table-column :width="150" dataIndex="billingType" title="开票类型">
          <template slot-scope="text,record">
            <ZgDicHolder :code="record.billingType" parentCode="billingType"/>
          </template>
        </a-table-column>
        <!-- <a-table-column :width="250" dataIndex="invoiceCode" title="发票代码"></a-table-column> -->
        <a-table-column dataIndex="invoiceNo" title="发票号码"></a-table-column>
        <a-table-column dataIndex="sellerName" title="销方名称"></a-table-column>
        <a-table-column dataIndex="sellerTaxNo" title="销方税号"></a-table-column>
        <a-table-column dataIndex="purchaserName" title="购方名称"></a-table-column>
        <a-table-column dataIndex="purchaserTaxNo" title="购方税号"></a-table-column>
        <a-table-column dataIndex="drawer" title="开票人"></a-table-column>
        <a-table-column dataIndex="amountWithTax" title="含税金额"></a-table-column>
        <a-table-column dataIndex="drewDate" title="开票日期"></a-table-column>
        <a-table-column dataIndex="pdfUrl" title="发票地址"></a-table-column>
      </template>
      <template slot="add">
        <Add ref="add"></Add>
      </template>
      <template slot="update" slot-scope="scope">
        <Update ref="update" :jsonStr="scope.jsonStr"></Update>
      </template>
      <template slot="condition">
        <Condition ref="condition"></Condition>
      </template>
    </ZgPage>
  </div>
</template>
<script>
import Add from './Add'
import Condition from './Condition'
export default {
  components: {
    Add, Condition
  },
  data () {
    return {
       pageOpt: {
        showConditionBtn: true,
        showDelBtn: false,
        showBatchBtn: false,
        showBatchAddBtn: false,
        showHeaderConfig: false,
        showLayoutMode: false,
        showUpdateBtn: false,
        conditionHeight: 180,
        manageFilter: true,
        showBtn: true
      }
    }
  },
  methods: {
    onSearch (param) {
      param.sourceSystem = '结算'
      param.userId = '结算'
      this.$http.post('/zg-invoice/open/invoice/page', param, data => {
        this.$refs.page.endSearch(
          {
            total: parseInt(data.total),
            list: data.records.map(t => {
              t.key = t.id
              return t
            })
          }
        )
      })
    },
    onAdd () {
      const result = this.$refs.add.save()
      if (!result.isOk) {
        this.$error({ title: result.msg })
        return
      }
      this.$http.post('/zg-invoice/open/invoice/issue', result.data, () => {
        this.$refs.page.hideAdd()
        this.$refs.page.search()
      })
    },
    onResetCondition () {
      this.$refs.condition.reset()
    },
    onSearchWithCondition (param) {
       param.sourceSystem = '结算'
      param.userId = '结算'
      const condition = this.$refs.condition.getParam()
      this.$http.post('/zg-invoice/open/invoice/page', Object.assign({}, param, condition), data => {
        this.$refs.page.endSearch(
          {
            total: parseInt(data.total),
            list: data.records.map(t => {
              t.key = t.id
              return t
            })
          }
        )
      })
    },
    onExtraBtnClick ({ key, obj }) {
      if (key === 'redInvoice') {
          const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_'
      let result = ''
      for (let i = 0; i < 32; i++) {
        const randomIndex = Math.floor(Math.random() * chars.length)
        result += chars[randomIndex]
      }
      this.$http.get('/zg-invoice/open/invoice/red', { invoiceNo: obj.invoiceNo, sourceSystem: '结算', extensionNum: '', externalDocumentNo: result }, data => {
        this.$success({ title: '红冲申请成功' })
      })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
